/*
 * Copyright 2002-2006 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and limitations under the
 * License.
 */
package pikes.html.xhtml.table;

import java.util.LinkedList;
import java.util.List;

import pikes.html.xhtml.Block;
import pikes.html.xhtml.CoreAttributes;
import pikes.html.xhtml.Events;
import pikes.html.xhtml.I18N;
import pikes.html.xhtml.NonRootTagFactory;
import pikes.util.Assert;
import pikes.xml.XMLTag;

/**
 * <code>&lt;table/&gt;</code> tag, see <a href="http://www.w3.org/TR/html401/interact/forms.html#h-17.3">TABLE
 * element</a> in HTML specification.
 * @author Peter Bona
 * @see <a href="http://www.w3.org/TR/html401/interact/forms.html#h-17.3">The TABLE element in HTML specification</a>
 */
public class Table extends NonRootTagFactory implements Block, CoreAttributes, I18N, Events {

	private THead head = null;

	private List<TBody> bodies = new LinkedList<TBody>();

	public Table(TR firstRow) {
		Assert.notNull(firstRow);
		bodies.add(new TBody(firstRow));
	}

	public Table(TBody tbody) {
		Assert.notNull(tbody);
		bodies.add(tbody);
	}

	/**
	 * @return "table"
	 */
	@Override
	protected CharSequence getTagName() {
		return "table";
	}

	@Override
	protected void decorateCreatedTag(XMLTag tag) {
		if (head != null) {
			head.decorateTag(tag);
		}

		for (TBody tbody : bodies) {
			tbody.decorateTag(tag);
		}
	}

	public void setHeader(THead thead) {
		this.head = thead;
	}

	public void add(TBody tbody) {
		if (tbody != null) {
			bodies.add(tbody);
		}
	}
}
